package com.aistar.service.employer.impl;

import com.aistar.mapper.*;
import com.aistar.model.dto.employer.InterviewDTO;
import com.aistar.model.dto.employer.TagAndTypeDTO;
import com.aistar.model.entity.*;
import com.aistar.service.employer.IEmployerRecruitmentService;
import com.aistar.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: EmployerRecruitmentServiceImpl
 * @author: LiuWenXiu
 * @date: 2021/10/1 15:56
 * @Description: TODO...本类用来演示:
 */
@Service
@Transactional
public class EmployerRecruitmentServiceImpl implements IEmployerRecruitmentService {

    @Autowired
    private EmployerMapper employerMapper;

    @Autowired
    private RecruitmentMapper recruitmentMapper;

    @Autowired
    private InterviewMapper interviewMapper;

    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private RecruitmentLabelMapper recruitmentLabelMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private LabelTypeMapper labelTypeMapper;

    @Override
    public List<Recruitment> loadMyPostRecruitment(Integer employerId) {
        // 首先获取到我的公司的id
        Integer companyId = employerMapper.findCompanyIdById(employerId);
        // 根据公司id获取所有该公司的职位
        List<Recruitment> recruitmentList = recruitmentMapper.findByCompanyId(companyId);
        // 返回出去
        return recruitmentList;
    }

    @Override
    public List<Recruitment> loadMyPostRecruitmentByPage(Integer employerId, Integer pageNum, Integer pageSize) {
        // 加载我发布的职位信息
        // 首先获取到我的公司的id
        Integer companyId = employerMapper.findCompanyIdById(employerId);
        // 根据公司id获取所有该公司的分页职位
        pageNum = (pageNum - 1) * pageSize;
        return recruitmentMapper.findByCompanyIdAndPage(companyId, pageNum, pageSize);
    }

    @Override
    public Recruitment getRecruitmentById(Integer id) {
        return recruitmentMapper.findById(id);
    }

    @Override
    public int updateReleaseTimeById(Integer id) {
        // 获取当前时间
        Date date = new Date();
        // 更新职位时间
        return recruitmentMapper.updateRecruitmentTimeById(id, date);
    }

    @Override
    public int updateRecruitmentById(Recruitment recruitment, String tagIds) {
        // 首先获取职位id
        Integer id = recruitment.getId();
        // 获取职位实体
        Recruitment oldRecruitment = recruitmentMapper.findById(id);
        // 修改属性
        // 因为在职位表中有些不在表单信息中,例如->发布时间,浏览次数,公司名称和状态
        // 所以要将原来的值设置进去
        recruitment.setReleaseTime(new Date());
        recruitment.setFrequency(oldRecruitment.getFrequency());
        recruitment.setCompanyId(oldRecruitment.getCompanyId());
        recruitment.setStatus(oldRecruitment.getStatus());
//        System.out.println(recruitment);
        // 调用更新方法
        int count = recruitmentMapper.updateRecruitment(recruitment);

        // 获取原来的标签id
        List<Integer> oldIds = recruitmentLabelMapper.findTagId(id);


        // 接下来要分为四种情况
        // 如果新的标签没有数据
        if (null == tagIds || tagIds.length() == 0) {
            // 如果原来的标签id为null
            if (null == oldIds || oldIds.size() == 0) {
                return count;
            }

            // 如果原来的标签id不为null - 需要删除原来的标签id
            for (Integer oldId : oldIds) {
                recruitmentLabelMapper.deleteBy(id, oldId);
            }
            return count;
        }
//        System.out.println(tagIds);
//        System.out.println(tagIds.length());

        // 到这新的标签有数据
        // 分割tagIds
        // 获取到了所有标签id
        String[] ids = tagIds.split(",");
        List<Integer> idArr = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            idArr.add(Integer.valueOf(ids[i]));
        }


        // 如果旧的标签没数据,则只要进行增加操作
        if (null == oldIds || oldIds.size() == 0) {
            for (Integer integer : idArr) {
                RecruitmentLabel recruitmentLabel = new RecruitmentLabel();
                recruitmentLabel.setRecruitmentId(id);
                recruitmentLabel.setTagId(integer);
                recruitmentLabelMapper.insertRecruitmentLabel(recruitmentLabel);
            }
            return count;
        }


        // 走到这一步表示旧的标签和新的标签都有数据

        // 需要删除的id - 原来的标签列表有,新的标签列表没有 - 遍历原来的标签,如果新的数组包含则无事,如果新的数组没有则需要删除
        List<Integer> delId = new ArrayList<>();
        for (Integer oldId : oldIds) {
            if (!idArr.contains(oldId)) {
                delId.add(oldId);
            }
        }
        // 需要创建的id - 原来的标签列表没有,新的标签列表有 - 遍历新的标签,如果原来的标签包含则无事,如果原来的标签没有则需要新建
        List<Integer> insertId = new ArrayList<>();
        for (Integer integer : idArr) {
            if (!oldIds.contains(integer)) {
                insertId.add(integer);
            }
        }

//        System.out.println("需要删除的标签");
//        System.out.println(delId);
        // 进行删除操作
        if (null != delId && delId.size() > 0) {
            for (Integer integer : delId) {
                recruitmentLabelMapper.deleteBy(id, integer);
            }
        }

//        System.out.println("需要添加的标签id");
//        System.out.println(insertId);
        if (null != insertId && insertId.size() > 0) {
            for (Integer integer : insertId) {
                RecruitmentLabel recruitmentLabel = new RecruitmentLabel();
                recruitmentLabel.setRecruitmentId(id);
                recruitmentLabel.setTagId(integer);
                recruitmentLabelMapper.insertRecruitmentLabel(recruitmentLabel);
            }
        }

        return count;
    }

    @Override
    public int delRecruitmentById(Integer id) {
        // 删除职位的同时要删除该职位所有的标签
        // 调用mapper方法
        int count = recruitmentMapper.delRecruitmentById(id);
        count += recruitmentLabelMapper.deleteByRecruitment(id);
        return count;
    }

    @Override
    public int delRecruitmentBatchById(String ids) {
        String[] id = ids.split(",");
        Integer[] idArr = new Integer[id.length];
        // 遍历数组
        for (int i = 0; i < id.length; i++) {
            idArr[i] = Integer.valueOf(id[i]);
        }
        int count = 0;
        // 遍历数组,多次调用mapper方法
        for (Integer i : idArr) {
            count += recruitmentMapper.delRecruitmentById(i);
        }
        return count;
    }

    @Override
    public List<InterviewDTO> loadInterview(Integer id) {
        List<InterviewDTO> interviewDTOList = new ArrayList<>();
        // 首先获取该招聘者所在公司id
        Integer companyId = employerMapper.findCompanyIdById(id);
        // 获取该公司的所有职位信息
        List<Recruitment> recruitmentList = recruitmentMapper.findByCompanyId(companyId);
        // 判断
        if (null == recruitmentList || recruitmentList.size() == 0)
            return null;
        // 走到这表示是有职位的,遍历职位,根据职位加载职位邀约信息
        for (Recruitment recruitment : recruitmentList) {
            List<Interview> interviews = interviewMapper.findByRecruitmentId(recruitment.getId());
            if (null != interviews && interviews.size() > 0) {
                // 遍历
                for (Interview interview : interviews) {
                    InterviewDTO interviewDTO = new InterviewDTO();
                    interviewDTO.setInterview(interview);
                    interviewDTO.setResume(resumeMapper.findById(interview.getResumeId()));
                    interviewDTO.setRecruitmentName(recruitmentMapper.findByName(interview.getRecruitmentId()));

                    interviewDTOList.add(interviewDTO);
                }
            }
        }
        // 需要对其进行排序
        // 匿名内部类
//        interviewDTOList.sort(new Comparator<InterviewDTO>() {
//            @Override
//            public int compare(InterviewDTO o1, InterviewDTO o2) {
//                if(o1.getInterview().getInterviewTime().getTime()>o2.getInterview().getInterviewTime().getTime())
//                    return -1;
//                else if(o1.getInterview().getInterviewTime().getTime()<o2.getInterview().getInterviewTime().getTime())
//                    return 1;
//                return 0;
//            }
//        });
        // lambda表达式
        interviewDTOList.sort((o1,o2)->{
            if(o1.getInterview().getInterviewTime().getTime()>o2.getInterview().getInterviewTime().getTime())
                    return -1;
                else if(o1.getInterview().getInterviewTime().getTime()<o2.getInterview().getInterviewTime().getTime())
                    return 1;
                return 0;
        });
        return interviewDTOList;
    }

    @Override
    public List<InterviewDTO> loadInterviewByPage(Integer id, Integer page, Integer limit) {
        List<InterviewDTO> interviewDTOListAll = loadInterview(id);
        if (null == interviewDTOListAll || interviewDTOListAll.size() == 0)
            return null;
        List<InterviewDTO> interviewDTOList = new ArrayList<>();
        // 上限
        Integer upper = (page - 1) * limit;
        // 下限
        Integer lower = page * limit;
        // 临时参数
        Integer temp = 0;
        for (InterviewDTO interviewDTO : interviewDTOListAll) {
            if (temp >= upper && temp < lower) {
                interviewDTOList.add(interviewDTO);
            }
            temp++;
        }
        return interviewDTOList;
    }

    @Override
    public List<Tag> loadTagByRecruitmentId(Integer recruitmentId) {
        // 首先根据职位id获取该职位对应的标签id
        List<Tag> tagList = new ArrayList<>();
        List<RecruitmentLabel> recruitmentLabelList = recruitmentLabelMapper.FindRecruitment(recruitmentId);
        // 判断该职位是否有标签
        if (null == recruitmentLabelList || recruitmentLabelList.size() == 0)
            return null;
        // 走到这一步表示该职位有标签
        // 遍历集合,获取所有的标签
        for (RecruitmentLabel recruitmentLabel : recruitmentLabelList) {
            Tag tag = tagMapper.getById(recruitmentLabel.getTagId());
            tagList.add(tag);
        }
        return tagList;
    }

    @Override
    public List<TagAndTypeDTO> loadTagByType() {
        List<TagAndTypeDTO> tagAndTypeDTOList = new ArrayList<>();
        // 首先查找所有的标签类别
        List<LabelType> labelTypeList = labelTypeMapper.findAll();
        if (null == labelTypeList || labelTypeList.size() == 0)
            return null;

        // 遍历标签类别表
        for (LabelType labelType : labelTypeList) {
            List<Tag> tagList = tagMapper.findByType(labelType.getId());
            TagAndTypeDTO tagAndTypeDTO = new TagAndTypeDTO();
            tagAndTypeDTO.setLabelType(labelType);
            tagAndTypeDTO.setTagList(tagList);
            tagAndTypeDTOList.add(tagAndTypeDTO);
        }
        return tagAndTypeDTOList;
    }

    @Override
    public int createNewRecruitment(Integer employerId, Recruitment recruitment, String tagIds) {

        //  根据招聘者id获取公司id
        Integer companyId = employerMapper.findCompanyIdById(employerId);
        // 页面传递过来的还有一些数据需要自己设置
        recruitment.setReleaseTime(new Date());
        recruitment.setStatus(1);
        recruitment.setFrequency(0);
        recruitment.setCompanyId(companyId);

        recruitmentMapper.insertRecruitment(recruitment);
        // 获取到了所有标签id
        Integer recruitmentId = recruitment.getId();

        if (null != tagIds && tagIds.length() > 0) {
            // 分割tagIds
            String[] id = tagIds.split(",");
            Integer[] idArr = new Integer[id.length];
            // 遍历数组
            for (int i = 0; i < id.length; i++) {
                idArr[i] = Integer.valueOf(id[i]);
            }

            // 调用RecruitmentLabelMapper方法进行插入
            for (Integer integer : idArr) {
                RecruitmentLabel recruitmentLabel = new RecruitmentLabel();
                recruitmentLabel.setTagId(integer);
                recruitmentLabel.setRecruitmentId(recruitmentId);
                recruitmentLabelMapper.insertRecruitmentLabel(recruitmentLabel);
            }
        }


        return recruitmentId;
    }

//    @Override
//    public List<Tag> loadAllTag() {
//        return tagMapper.findAll();
//    }
//
//    @Override
//    public List<Tag> loadTagByType(Integer id) {
//        return tagMapper.findByType(id);
//    }


}
